home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d18 / vis082s.arc / VGA256.PAS < prev    next >
Pascal/Delphi Source File  |  1991-04-17  |  3KB  |  116 lines

  1. {-------------------------------------------------------------------}
  2. { A simple program to illustrate how to plot pixels and draw lines  }
  3. { in the 320x200 256 color mode available on the VGA and MCGA cards }
  4. {                                                                   }
  5. { Last update: 5/24/88 by John Sieraski 76117,2022                  }
  6. {-------------------------------------------------------------------}
  7.  
  8. {$R-,S-}
  9.  
  10. uses
  11.   Crt, Dos;
  12.  
  13. procedure PutPixel(X, Y : word; Color : byte);
  14. { Plot a pixel at (X, Y) in Color }
  15. begin
  16.   Mem[$A000:Y*320+X] := Color;
  17. end;
  18.  
  19. procedure Switch(var First, Second : integer);
  20. { Exchange the values of First and second }
  21. var
  22.   Temp : integer;
  23. begin
  24.   Temp := First;
  25.   First := Second;
  26.   Second := Temp;
  27. end; { Switch }
  28.  
  29. procedure Line(X1, Y1, X2, Y2, Color : integer);
  30. { Uses Bressenham's algorithm for drawing a line }
  31. var
  32.   LgDelta, ShDelta, LgStep, ShStep, Cycle, PointAddr : integer;
  33.  
  34. begin
  35.   LgDelta := X2 - X1;
  36.   ShDelta := Y2 - Y1;
  37.   if LgDelta < 0 then
  38.     begin
  39.       LgDelta := -LgDelta;
  40.       LgStep := -1;
  41.     end
  42.   else
  43.     LgStep := 1;
  44.   if ShDelta < 0 then
  45.     begin
  46.       ShDelta := -ShDelta;
  47.       ShStep := -1;
  48.     end
  49.   else
  50.     ShStep := 1;
  51.   if LgDelta > ShDelta then
  52.     begin
  53.       Cycle := LgDelta shr 1; { LgDelta / 2 }
  54.       while X1 <> X2 do
  55.       begin
  56.         Mem[$A000:Y1*320+X1] := Color; { PutPixel(X1, Y1, Color); }
  57.         Inc(X1, LgStep);
  58.         Inc(Cycle, ShDelta);
  59.         if Cycle > LgDelta then
  60.         begin
  61.           Inc(Y1, ShStep);
  62.           Dec(Cycle, LgDelta);
  63.         end;
  64.       end;
  65.     end
  66.   else
  67.     begin
  68.       Cycle := ShDelta shr 1; { ShDelta / 2 }
  69.       Switch(LgDelta, ShDelta);
  70.       Switch(LgStep, ShStep);
  71.       while Y1 <> Y2 do
  72.       begin
  73.         Mem[$A000:Y1*320+X1] := Color; { PutPixel(X1, Y1, Color); }
  74.         Inc(Y1, LgStep);
  75.         Inc(Cycle, ShDelta);
  76.         if Cycle > LgDelta then
  77.         begin
  78.           Inc(X1, ShStep);
  79.           Dec(Cycle, LgDelta);
  80.         end;
  81.       end;
  82.     end;
  83. end; { Line }
  84.  
  85. procedure SetMode(Mode : byte);
  86. { Interrupt $10, sub-function 0 - Set video mode }
  87. var
  88.   Regs : Registers;
  89. begin
  90.   with Regs do
  91.   begin
  92.     AH := 0;
  93.     AL := Mode;
  94.   end;
  95.   Intr($10, Regs);
  96. end; { SetMode }
  97.  
  98. var
  99.   Row   : integer;
  100.   Color : byte;
  101.   Ch    : char;
  102. begin
  103.   SetMode($13);  { 320x200 256 color mode for VGA and MCGA cards }
  104.   Color := 0;
  105.   for Row := 0 to 199 do           { Draw some lines }
  106.   begin
  107.     if Odd(Row) then
  108.     begin
  109.       Line(0, Row, 319, Row, Color);
  110.       Inc(Color);
  111.     end;
  112.   end;
  113.   Ch := ReadKey;
  114.   TextMode(co80);
  115. end.
  116.